//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;


namespace SFWK.Core.Inspector
{
    /// <summary>
    /// Injects Code at the beginning and at the end of a service call
    /// </summary>
    public class InspectorAttribute : Attribute, IOperationBehavior
    {
        #region Enum
        /// <summary>
        /// Possible values for Type of operator passed to the service
        /// </summary>
        public enum EOperatorType
        {
            Operator,
            UserName,
            None
        }
        #endregion

        #region Properties
        /// <summary>
        /// Type of operator passed to the service
        /// </summary>
        public readonly EOperatorType OperatorType;

        /// <summary>
        /// Position of the operator parameter
        /// </summary>
        public readonly int Position;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor with OperatorType
        /// </summary>
        /// <param name="operatorType"></param>
        public InspectorAttribute(EOperatorType operatorType, int position)  // url is a positional parameter
        {
            this.OperatorType = operatorType;
            this.Position = position;
        }

        /// <summary>
        /// Default Constructor
        /// </summary>
        public InspectorAttribute()  // url is a positional parameter
        {
            this.OperatorType = EOperatorType.Operator;
            this.Position = 0;
        }
        #endregion


        #region IOperationBehavior Members
        /// <summary>
        /// Apply injection on services
        /// Called in WCF remote call mode only
        /// </summary>
        /// <param name="operationDescription"></param>
        /// <param name="dispatchOperation"></param>
        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.ParameterInspectors.Add(new InspectorParameter(this.OperatorType, this.Position));
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
        }

        public void Validate(OperationDescription operationDescription)
        {
        }
        #endregion
    }
}

